package com.ellison.demo.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ellison.demo.common.entity.WorkDoc;
import com.ellison.demo.common.mapper.WorkDocMapper;
import com.ellison.demo.common.utils.StringUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ellison Pei
 */
@Service
public class WorkDocService{

    @Resource
    private WorkDocMapper workDocMapper;

    public List<WorkDoc> pages(String relatedBillCode){
        List<WorkDoc> result = Lists.newArrayList();
        LambdaQueryWrapper<WorkDoc> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkDoc::getRelatedBillCode, relatedBillCode);
        List<WorkDoc> workDocs = workDocMapper.selectList(queryWrapper);
//        Collections.sort(workDocs, (o1, o2) -> o2.getCheckTime().compareTo(o1.getCheckTime()));
//        Collections.sort(workDocs, Comparator.comparing(WorkDoc::getCheckTime));

//        //1
//        List<WorkDoc> workdoc1 = Lists.newArrayList();
//        //2
//        List<WorkDoc> workdoc2 = Lists.newArrayList();
//        //0
//        List<WorkDoc> workdoc = Lists.newArrayList();
//        List<WorkDoc> collect3 = workDocs.stream()
//                .distinct()
//                .filter(workDoc -> StringUtils.isNotEmpty(workDoc.getAllocatedQuantity().toString()))
//                .peek(workDoc -> {
//                    if (workDoc.getAllocatedQuantity().compareTo(new BigDecimal("1")) == 0) {
//                        workdoc1.add(workDoc);
//                    } else if (workDoc.getAllocatedQuantity().compareTo(new BigDecimal("2")) == 0) {
//                        workdoc2.add(workDoc);
//                    } else {
//                        workdoc.add(workDoc);
//                    }
//                })
//                .collect(Collectors.toList());
//
        result = workDocs.stream()
                .filter(workDoc -> StringUtils.isNull(workDoc.getCustomerId()))
                .peek(w -> w.setCustomerId(1000L))
                .collect(Collectors.toList());
//
//        List<String> collect = workDocs.stream().map(WorkDoc::getFactory).distinct().collect(Collectors.toList());
//        Map<String, WorkDoc> collect1 = workDocs.stream()
//                .distinct()
//                .collect(Collectors.toMap(WorkDoc::getWorkDocCode, workDoc -> workDoc, (oldValue, newValue) -> oldValue));
//        WorkDoc po2108270017 = collect1.get("PO2108270017");
//        Map<String, List<WorkDoc>> collect2 = workDocs.stream()
//                .distinct()
//                .collect(Collectors.groupingBy(WorkDoc::getFactory));

        return  result;
    }

}
